# Maintainer: Christopher Degawa <ccom@randomderp.com>

_realname=ripgrep
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=14.1.1
pkgrel=1
pkgdesc='line-oriented search tool that recursively searches your current directory for a regex pattern (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/BurntSushi/ripgrep'
msys2_references=(
  'archlinux: ripgrep'
  'purl: pkg:cargo/ripgrep'
  "cpe: cpe:2.3:a:ripgrep_project:ripgrep"
)
license=('spdx:MIT OR Unlicense')
depends=("${MINGW_PACKAGE_PREFIX}-pcre2")
makedepends=("${MINGW_PACKAGE_PREFIX}-rust" "${MINGW_PACKAGE_PREFIX}-pkgconf")
options=('!strip')
source=("${url}/archive/${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('4dad02a2f9c8c3c8d89434e47337aa654cb0e2aa50e806589132f186bf5c2b66')
noextract=("${_realname}-${pkgver}.tar.gz")

prepare() {
  tar -xzf "${_realname}-${pkgver}.tar.gz" || true
  cd "${srcdir}/${_realname}-${pkgver}"

  cargo fetch --locked --target "$(rustc -vV | sed -n 's/host: //p')"
}

build() {
  cd "${_realname}-${pkgver}"

  cargo build --frozen --features 'pcre2' --profile release-lto
}

check() {
  cd "${_realname}-${pkgver}"

  cargo test --frozen --features 'pcre2' --profile release-lto
}

package() {
  cd "${srcdir}/${_realname}-${pkgver}"

  cargo install \
    --frozen \
    --offline \
    --no-track \
    --features 'pcre2' \
    --profile release-lto \
    --path . \
    --root "${pkgdir}${MINGW_PREFIX}"

  local _complete="./target/release-lto/rg --generate"
  $_complete complete-zsh | install -Dm644 /dev/stdin "${pkgdir}${MINGW_PREFIX}/share/zsh/site-functions/_rg"
  $_complete complete-bash | install -Dm644 /dev/stdin "${pkgdir}${MINGW_PREFIX}/share/bash-completion/completions/rg"
  $_complete complete-fish | install -Dm644 /dev/stdin "${pkgdir}${MINGW_PREFIX}/share/fish/vendor_completions.d/rg.fish"
  $_complete man | install -Dm644 /dev/stdin "${pkgdir}${MINGW_PREFIX}/share/man/man1/rg.1"

  install -Dm644 COPYING LICENSE-MIT UNLICENSE -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/"
}
